package phone.example.com.a36kr.main;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;

import cn.bmob.v3.Bmob;
import phone.example.com.a36kr.R;
import phone.example.com.a36kr.adapter.ViewPagerAdapter;
import phone.example.com.a36kr.bean.UrlBean;
import phone.example.com.a36kr.fragment.FindFragment;
import phone.example.com.a36kr.fragment.InvestFragment;
import phone.example.com.a36kr.fragment.MyFragment;
import phone.example.com.a36kr.fragment.NewsFragment;
import phone.example.com.a36kr.tools.BaseActivity;

public class MainActivity extends BaseActivity implements View.OnClickListener {


    private TabLayout tabLayout;
    private ViewPager viewPager;
    private DrawerLayout drawerLayout;//抽屉
    private LinearLayout llAll; //全部
    private LinearLayout llEProject;//早起工程
    private LinearLayout llBWheel;//B轮
    private LinearLayout llBCompany;//大公司
    private LinearLayout llCapital;//资本
    private LinearLayout llDepth;//深度
    private LinearLayout llStudy;//研究
    private LinearLayout llKrTv;//氪TV


    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tabLayout = (TabLayout) findViewById(R.id.main_tabLayout);
        viewPager = (ViewPager) findViewById(R.id.main_viewPager);
        drawerLayout = (DrawerLayout) findViewById(R.id.main_dl);

        llAll = bindView(R.id.main_btnll_all);
        llEProject = bindView(R.id.main_btnll_earlyProject);
        llBWheel = bindView(R.id.main_btnll_BWheel);
        llBCompany = bindView(R.id.main_btnll_bigCompany);
        llCapital = bindView(R.id.main_btnll_capital);
        llDepth = bindView(R.id.main_btnll_depth);
        llStudy = bindView(R.id.main_btnll_study);
        llKrTv = bindView(R.id.main_btnll_krTv);

        llAll.setOnClickListener(this);
        llEProject.setOnClickListener(this);
        llBWheel.setOnClickListener(this);
        llBCompany.setOnClickListener(this);
        llCapital.setOnClickListener(this);
        llDepth.setOnClickListener(this);
        llStudy.setOnClickListener(this);
        llKrTv.setOnClickListener(this);
        LinearLayout linearLayout = bindView(R.id.main_ll_ll);
        linearLayout.bringToFront(); // 点击按钮才消失 点击布局不会消失


        // Bmob 默认初始化
        Bmob.initialize(this, "c96bf5415d33b2ceb521cc0a47f56cce");

    }

    public void openMenu(){
        drawerLayout.openDrawer(Gravity.LEFT);
    }

    public void unLockedMenu(){
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);//打开手势滑动
    }
    public void lockedMenu(){
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);//关闭手势滑动
    }

    @Override
    protected void initData() {
        ArrayList<Fragment>fragments = new ArrayList<>();
        fragments.add(new NewsFragment()); //新闻
        fragments.add(new InvestFragment());// 投资
        fragments.add(new FindFragment());// 发现
        fragments.add(new MyFragment());//我的
        ViewPagerAdapter krPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        krPagerAdapter.setFragments(fragments);
        viewPager.setAdapter(krPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        // 更改tabLayout字体颜色
        tabLayout.setTabTextColors(Color.rgb(0x98, 0x9d, 0xa8), Color.rgb(0x6c, 0x8f, 0xff));

        tabLayout.getTabAt(0).setIcon(R.drawable.news);
        tabLayout.getTabAt(1).setIcon(R.drawable.inves);
        tabLayout.getTabAt(2).setIcon(R.drawable.find);
        tabLayout.getTabAt(3).setIcon(R.drawable.my);

        /**********viewPager和tabLayout*********/

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position == 0){
                    unLockedMenu();
                } else {
                  lockedMenu();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });



    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent();
        intent.setAction("com.36kr.android.newsReplaceUrl");
        switch (v.getId()){
            case R.id.main_btnll_all:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_ALL_URL);
                intent.putExtra("newsSelectUrl", "all&pagingAction=up");
                break;
            case R.id.main_btnll_earlyProject:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_EPROJECT_URL);
                intent.putExtra("newsSelectUrl", "67&pagingAction=up");
                break;
            case R.id.main_btnll_BWheel:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_BWHEEL_URL);
                intent.putExtra("newsSelectUrl", "68&pagingAction=up");
                break;
            case R.id.main_btnll_bigCompany:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_BCOMPANY_URL);
                intent.putExtra("newsSelectUrl", "23&pagingAction=up");
                break;
            case  R.id.main_btnll_capital:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_CAPITAL_URL);
                intent.putExtra("newsSelectUrl", "69&pagingAction=up");
                break;
            case R.id.main_btnll_depth:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_DEPTH_URL);
                intent.putExtra("newsSelectUrl", "70&pagingAction=up");
                break;
            case R.id.main_btnll_study:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_STUDY_URL);
                intent.putExtra("newsSelectUrl", "71&pagingAction=up");
                break;
            case R.id.main_btnll_krTv:
                intent.putExtra("newsReplaceUrl", UrlBean.NEWS_KRTY_URL);
                break;
        }
        drawerLayout.closeDrawers();
        sendBroadcast(intent);


    }

}
